import { getdevicebystructure, getView } from '../../../api/getData.js';
const App = getApp()
Component({
  properties: {
    deviceList: {
      type: Object
    }
  },

  data: {
    deatilNav: [], // 面包屑
    multiArray: [], // 选择面板设备数组
    showDialog: 'hidden', // 是否显示选择面板
    selectedObj: {}, // 选择面板选中的那个对象
  },
  lifetimes: {
    ready() {
      this.data.deviceList.map((item) => {
        this.data.multiArray.push(item.deviceName)
      })
      if (App.globalData.deatilNav[App.globalData.deatilNav.length - 1] === '') {
        App.globalData.deatilNav[App.globalData.deatilNav.length - 1] = App.globalData.deviceName.deviceName
      } else {
        App.globalData.deatilNav.push(App.globalData.deviceName.deviceName)
      }
      this.setData({
        multiArray: this.data.multiArray,
        deatilNav: App.globalData.deatilNav
      })
    },
  },
  methods: {
    bindPickerChange: function (e) {
      this.setData({
        index: e.detail.value,
        selectedObj: this.data.deviceList[e.detail.value[0]]
      });
      App.globalData.deviceName = {
        deviceID: this.data.selectedObj.deviceID,
        deviceName: this.data.selectedObj.deviceName
      }
      App.globalData.deviceID = this.data.selectedObj.deviceID;
      App.globalData.deatilNav[App.globalData.deatilNav.length - 1] = this.data.selectedObj.deviceName;
      this.setData({
        deatilNav: App.globalData.deatilNav
      })
      wx.setStorageSync('moduleList_new', this.data.selectedObj.template.moduleList)
      wx.setStorageSync('moduleConfig_new', this.data.selectedObj.template.moduleConfig)
      this.triggerEvent("sendSocketMsg", this.data.selectedObj);
    },
    // 选择picker
    chooseDevice(e) {
      this.setData({
        selectedObj: this.data.deviceList[e.detail.value[0]]
      })
    },

    // picker确定
    surePicker() {
      App.globalData.showPicker = false;
      if (!Object.keys(this.data.selectedObj).length) { // 针对未选择的情况
        this.showDialogFn(0);
        return
      }
     
      this.showDialogFn(0);
    },
  }
})